<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:x="https://www.texmacs.org/2002/extensions" xmlns:m="http://www.w3.org/1998/Math/MathML">
  <head>
    <title>TeXmacs notes</title>
    <meta content="TeXmacs 1.99.15" name="generator"></meta>
    <link href="../resources/notes-base.css" type="text/css" rel="stylesheet"></link>
    <link href="../resources/blog-icon.png" rel="icon"></link>
    <script src="../resources/highlight.pack.js" language="javascript" defer></script>
    <script src="../resources/notes-base.js" language="javascript" defer></script>
  </head>
  <body>
    <div class="notes-header">
      <p>
        <img class="image" src="../resources/texmacs-blog-transparent.png" width="28.116784"></img><span style="margin-left: 2pt"></span><a href="./main.html">[main]</a><em
        class="notes-header-name">Notes on TeXmacs</em>
      </p>
    </div>
    <h1 id="auto-1">Embedding graphics composed with Scheme into documents<span style="margin-left: 1em"></span></h1>
    <p>
      In a <a href="./scheme-graphics.html">previous note</a>, we have shown an example of TeXmacs
      native graphics generated with <class style="font-variant: small-caps">Scheme</class>, a triangle
      inscribed in a half-circle.
    </p>
    <p>
      Now let us see how to embed it seamlessly in a document. Our work is
      based on <class style="font-variant: small-caps">Fold</class> <class style="font-variant: small-caps">Executable</class>
      environments, available under <class class="tmweb-menu" style="font-family: sans-serif">Insert</class>&rarr;<class
      class="tmweb-menu" style="font-family: sans-serif">Fold</class>&rarr;<class class="tmweb-menu" style="font-family: sans-serif">Executable</class><a
      id="auto-2"></a> (and then choose the <class style="font-variant: small-caps">Scheme</class> option).
    </p>
    <p>
      Since in each <class style="font-variant: small-caps">Executable</class> environment it is
      possible to execute one <class style="font-variant: small-caps">Scheme</class> instruction only,
      we will wrap all of the code inside a <tt class="verbatim scheme">begin</tt> form.
    </p>
    <p>
      For more complex drawings users may feel the need of more efficient
      facilities to compose, test and deploy into TeXmacs their code. We are
      going to discuss some available tools in a future post.
    </p>
    <p>
      A synthetic discussion of TeXmacs graphics primitives and how to code
      them in <class style="font-variant: small-caps">Scheme</class> is in the <a href="./scheme-graphics.html">previous
      note</a> (see also the manual, in <class class="tmweb-menu" style="font-family: sans-serif">Help</class>&rarr;<class
      class="tmweb-menu" style="font-family: sans-serif">Manual</class>&rarr;<class class="tmweb-menu" style="font-family: sans-serif">Creating
      technical pictures</class><a id="auto-3"></a>). Here let us just remind that
      TeXmacs graphics are composed by listing graphical objects, each made by
      the application of a graphical primitive, inside a <font class="tmweb-markup">graphics</font>
      primitive.
    </p>
    <p>
      In the code we ue in this note, we introduce a function <tt class="verbatim scheme">pt</tt>
      that generates <font class="tmweb-markup">point</font> primitives, (which are
      parametrized by two numbers, expressed as strings), and with it we
      generate points that we will use to build other primitives (<font class="tmweb-markup">arc</font>,
      <font class="tmweb-markup">line</font>, <font class="tmweb-markup">cline</font> and <font class="tmweb-markup">text-at</font>).
    </p>
    <p>
      In turn, TeXmacs will use the primitives to represent the triangle, the
      half-circle and the decorations, that is the drawing that we will embed
      in the document.
    </p>
    <p>
      We open the environment with <class class="tmweb-menu" style="font-family: sans-serif">Insert</class>&rarr;<class
      class="tmweb-menu" style="font-family: sans-serif">Fold</class>&rarr;<class class="tmweb-menu" style="font-family: sans-serif">Executable</class>&rarr;<class
      class="tmweb-menu" style="font-family: sans-serif">Scheme</class><a id="auto-4"></a> and we obtain the yellow box we
      got used to in the note on <a href="./embedding-tikz-figures-short.html">embedding TikZ graphics</a>, in this
      case introduced by a <samp>Scheme</samp> title on a gray background:
    </p>
    <p>
      <font color="grey"><table style="display: inline-table; vertical-align: middle">
        <tbody><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #8080A0; padding-left: 0.15em; padding-right: 0.15em"><font style="font-size: 90.0%"><class style="font-family: sans-serif"><font color="white">Scheme</font></class></font></td>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #ffff0020"><font color="black"><tt class="verbatim"></tt></font></td>
        </tr></tbody>
      </table></font>
    </p>
    <p>
      Press <font class="tmweb-key">&#x21E7;Return</font> and type or paste the graphics
      command; in this note let us copy the same commands that generate the
      drawing in the <a href="./scheme-graphics.html">previous note</a>, wrapping everything in a <tt
      class="verbatim scheme">begin</tt> form as we already said:
    </p>
    <p>
      <font color="grey"><table style="display: inline-table; vertical-align: middle">
        <tbody><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #8080A0; padding-bottom: 0.15em; padding-top: 0.15em; text-align: center"><font style="font-size: 90.0%"><class style="font-family: sans-serif"><font color="white">Scheme</font></class></font></td>
        </tr><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #ffff0020; width: 100%; padding-top: 0.3em; padding-bottom: 0.3em"><font color="black"><div class="compact-block">
            <pre class="verbatim" xml:space="preserve">
(begin
(define pi (acos -1))

;; a function for generating TeXmacs points
(define (pt x y)
  &lsquo;(point ,(number-&gt;string x) ,(number-&gt;string y)))

;; points for the triangle
(define pA (pt -2 0))
(define pB (pt 2 0))
(define xC (- (* 2 (cos (/ pi 3))))); x-coordinate for point C
(define yC (* 2 (sin (/ pi 3)))); y-coordinate for point C
(define pC (pt xC yC))

;; points for the letters
(define tA (pt -2.3 -0.5))
(define tB (pt 2.1 -0.5))
(define tC (pt (- xC 0.2) (+ yC 0.2)))

(stree-&gt;tree
&lsquo;(with &quot;gr-geometry&quot; (tuple &quot;geometry&quot; &quot;400px&quot; &quot;300px&quot; &quot;center&quot;)
   (graphics
  ;; the arc and the line together make the semicircle
  (with &quot;color&quot; &quot;black&quot; (arc ,pA ,pC ,pB))
  (with &quot;color&quot; &quot;black&quot; (line ,pA ,pB))
  ;; a closed polyline for the triangle
  (with &quot;color&quot; &quot;red&quot;   (cline ,pA ,pB ,pC))
  ;; add letters using text-at
  (with &quot;color&quot; &quot;black&quot; (text-at &quot;A&quot; ,tA))  
  (with &quot;color&quot; &quot;black&quot; (text-at &quot;B&quot; ,tB))  
  (with &quot;color&quot; &quot;black&quot; (text-at &quot;C&quot; ,tC))
  ;; finally decorate with the TeXmacs symbol
  ;; and close all of the parentheses!!!
  (with &quot;color&quot; &quot;blue&quot;  (text-at (TeXmacs) ,(pt -0.55 -0.75)))))))</pre>
          </div></font></td>
        </tr></tbody>
      </table></font>
    </p>
    <p>
      The last step is pressing <font class="tmweb-key">&#x21E7;Return</font> to execute
      the code and generate the drawing. We do it in a <font class="tmweb-markup">big-figure</font>
      environment (<class class="tmweb-menu" style="font-family: sans-serif">Insert</class>&rarr;<class class="tmweb-menu"
      style="font-family: sans-serif">Image</class>&rarr;<class class="tmweb-menu" style="font-family: sans-serif">Big figure</class><a id="auto-5"></a>)
      to demonstrate seamless embedding:
    </p>
    <div style="margin-top: 1em; margin-bottom: 1em">
      <table style="width: 100%">
        <tbody><tr>
          <td style="text-align: center; padding-left: 0em; padding-right: 0em"><p>
            <img src="scheme-graphics-embedding-1.png" style="margin-left: -0.0206868686868687em; margin-bottom: 0em; margin-right: -0.020686868686866em; margin-top: 0em; vertical-align: -7.75757575757576em; height: 15.5151515151515em"></img>
          </p><p>
            
          </p></td>
        </tr><tr>
          <td style="text-align: center; padding-left: 0em; padding-right: 0em; height: 0.5em"></td>
        </tr><tr>
          <td style="text-align: center; padding-left: 0em; padding-right: 0em; padding-left: 1.5em; padding-right: 1.5em"><div class="caption">
            <font style="font-size: 90.0%"><p>
              <b>Figure 1. </b><a id="auto-6"></a>A drawing generated with <class
              style="font-variant: small-caps">Scheme</class>, embedded in a <font class="tmweb-markup">big-figure</font>
              environment
            </p></font>
          </div></td>
        </tr></tbody>
      </table>
    </div>
    <p>
      The drawing we generated is editable in two different ways.
    </p>
    <p>
      Placing the cursor at the drawing (just after or just before, the
      drawing is then surrounded by a thin cyan frame) and pressing <font
      class="tmweb-key">Return</font> brings back the yellow edit window, where the code
      can be changed and re-executed into a new drawing.
    </p>
    <p>
      <font color="grey"><table style="display: inline-table; vertical-align: middle">
        <tbody><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #8080A0; padding-bottom: 0.15em; padding-top: 0.15em; text-align: center"><font style="font-size: 90.0%"><class style="font-family: sans-serif"><font color="white">Scheme</font></class></font></td>
        </tr><tr>
          <td style="border-left: 0.5px solid; border-right: 0.5px solid; border-bottom: 0.5px solid; border-top: 0.5px solid; background-color: #ffff0020; width: 100%; padding-top: 0.3em; padding-bottom: 0.3em"><font color="black"><div class="compact-block">
            <pre class="verbatim" xml:space="preserve">
(begin
(define pi (acos -1))

;; a function for generating TeXmacs points
(define (pt x y)
  &lsquo;(point ,(number-&gt;string x) ,(number-&gt;string y)))

;; points for the triangle
(define pA (pt -2 0))
(define pB (pt 2 0))
(define xC (- (* 2 (cos (/ pi 3))))); x-coordinate for point C
(define yC (* 2 (sin (/ pi 3)))); y-coordinate for point C
(define pC (pt xC yC))

;; points for the letters
(define tA (pt -2.3 -0.5))
(define tB (pt 2.1 -0.5))
(define tC (pt (- xC 0.2) (+ yC 0.2)))

(stree-&gt;tree
&lsquo;(with &quot;gr-geometry&quot; (tuple &quot;geometry&quot; &quot;400px&quot; &quot;300px&quot; &quot;center&quot;)
   (graphics
  ;; the arc and the line together make the semicircle
  (with &quot;color&quot; &quot;black&quot; (arc ,pA ,pC ,pB))
  (with &quot;color&quot; &quot;black&quot; (line ,pA ,pB))
  ;; a closed polyline for the triangle
  (with &quot;color&quot; &quot;red&quot;   (cline ,pA ,pB ,pC))
  ;; add letters using text-at
  (with &quot;color&quot; &quot;black&quot; (text-at &quot;A&quot; ,tA))  
  (with &quot;color&quot; &quot;black&quot; (text-at &quot;B&quot; ,tB))  
  (with &quot;color&quot; &quot;black&quot; (text-at &quot;C&quot; ,tC))
  ;; finally decorate with the TeXmacs symbol
  ;; and close all of the parentheses!!!
  (with &quot;color&quot; &quot;blue&quot;  (text-at (TeXmacs) ,(pt -0.55 -0.75)))))))</pre>
          </div></font></td>
        </tr></tbody>
      </table></font>
    </p>
    <p>
      
    </p>
    <p>
      It is also possible to edit the drawing with the interactive (point and
      click) facilities. In this case too it is always possible to return to
      the text-editing mode of the <class style="font-variant: small-caps">Executable</class>
      environment by pressing <font class="tmweb-key">Return</font> with the cursor at
      the drawing, but if one does that, the interactive modifications are
      lost, i.e. one gets back to the <class style="font-variant: small-caps">Scheme</class> code one
      had typed into the <class style="font-variant: small-caps">Executable</class> environment.
    </p>
  </body>
</html>